/*

 * Created on 25.04.2005

 *

 * Created from Rainer Garbotz

 * Shows the Error in the setText() Method of JTextPane,

 * if it is used with HTMLEditorKit

 *

 * Problem Description:

 * Press the "Show Source Code" Button and you switch between HTML and Sourcecode

 * With every switch between the sights a part of the HTML-Code is delete (where is the gost?)

 *

 * Solution:

 * I inserted "0001" (search after that number or go to line 72). But that is crasy and I don`t

 * understand it! The Problem is, that the "

 " Tag after "</table>"

 *

 */

package garbotz.test;

import java.io.IOException;

import javax.swing.JFrame;

import javax.swing.JMenuItem;

import javax.swing.JScrollPane;

import javax.swing.JTextPane;

import javax.swing.text.BadLocationException;

import javax.swing.text.Element;

import javax.swing.text.html.HTML;

import javax.swing.text.html.HTMLDocument;

import javax.swing.text.html.HTMLEditorKit;

import javax.swing.text.html.HTMLEditorKit.Parser;

import javax.swing.text.html.HTMLEditorKit.ParserCallback;

public class Src_Html extends JFrame {

	private javax.swing.JPanel jContentPane = null;

	private javax.swing.JMenuBar jJMenuBar = null;

	private javax.swing.JMenu fileMenu = null;

	private javax.swing.JMenu editMenu = null;

	private javax.swing.JMenuItem exitMenuItem = null;

	private javax.swing.JMenuItem MIshowSRC = null;

	private HTMLEditorKit html;

	private JScrollPane jScrollPane = null;

	private JTextPane jTP_HTML = null;

	private JTextPane jTP_SRC = null; // @jve:decl-index=0:visual-constraint="102,471"

	private HTMLDocument htmlDoc;

	private Parser defaultParser;

	private JMenuItem insertMenu = null;

	private int egal = 1;

	private ExtTagAction eta = new ExtTagAction();

	/**
	 * 
	 * This method initializes jScrollPane
	 * 
	 * 
	 * 
	 * @return javax.swing.JScrollPane
	 * 
	 */

	private JScrollPane getJScrollPane() {

		if (jScrollPane == null) {

			jScrollPane = new JScrollPane();

			jScrollPane.setViewportView(getJTP_HTML());

		}

		return jScrollPane;

	}

	/**
	 * 
	 * This method initializes jTextPane
	 * 
	 * 
	 * 
	 * @return javax.swing.JTextPane
	 * 
	 */

	private JTextPane getJTP_HTML() {

		if (jTP_HTML == null) {

			jTP_HTML = new JTextPane();

			html = new HTMLEditorKit();

			jTP_HTML.setEditorKit(html);

			jTP_HTML.setContentType("text/html");

			// Insert \240 in the Table. Then the Columns wouldn`t delete, but
			// there is an
			// space in ever cell of // the table this couldn`t be an good
			// solution!

			String example = "<html><head></head><body>Nice Day today<br><table border=\"1\" width=\"100%\">";

			// +
			// "<tr>\240<td>\240<br><br></td><td>\240</td></tr><tr>\240<td>\240</td><td>\240</td></tr></table></body></html>";String
			// example = "<html><head></head><body>Nice Day
			// today<br></body></html>";

			jTP_HTML.setText(example);

		}

		// 0001: Helps a little bit

		if (egal > 1) {

			try {

				// jTP_HTML.setContentType("text/plain");

				jTP_HTML.setText(jTP_SRC.getText());

				jTP_HTML.setContentType("text/html");

			} catch (NullPointerException npe) {

				System.out.println(npe.getMessage());

			}

		}

		// 0001: End

		egal = 2;

		return jTP_HTML;

	}

	/**
	 * 
	 * This method initializes jTextPane1
	 * 
	 * 
	 * 
	 * @return javax.swing.JTextPane
	 * 
	 */

	private JTextPane getJTP_SRC() {

		if (jTP_SRC == null) {

			jTP_SRC = new JTextPane();

		}

		try {

			jTP_SRC.setText(jTP_HTML.getText());

		} catch (NullPointerException npe) {

			System.out.println(npe.getMessage());

		}

		return jTP_SRC;

	}

	/**
	 * 
	 * This method initializes jMenuItem
	 * 
	 * 
	 * 
	 * @return javax.swing.JMenuItem
	 * 
	 */

	private JMenuItem getInsertMenu() {

		if (insertMenu == null) {

			insertMenu = new JMenuItem();

			insertMenu.setText("Exit");

			insertMenu.addActionListener(new java.awt.event.ActionListener() {

				public void actionPerformed(java.awt.event.ActionEvent e) {

					Element el = eta.getCurrentTag(jTP_HTML);

					HTML.Tag ht = HTML.getTag(el.getName());

					// ExtTagAction eta = new ExtTagAction();

					// eta.getTagfromString(el.getName());

					HTMLDocument htmldoc = (HTMLDocument) jTP_HTML
							.getDocument();

					Element para = htmldoc.getParagraphElement(jTP_HTML
							.getSelectionStart());

					System.out.println("NAME:::" + para.getName());

					try {

						htmldoc.insertBeforeEnd(para, "<br>");

					} catch (BadLocationException e1) {

						// TODO Auto-generated catch block

						e1.printStackTrace();

					} catch (IOException e1) {

						// TODO Auto-generated catch block

						e1.printStackTrace();

					}

					// // Funktioniert zwar, allerdings k鰊nen die
					// Positionsparameter nicht genaus
					// dynamisch angegeben werden

					// try {

					// insertHTML(htmldoc, jTP_HTML.getCaretPosition(),
					// "<br>",1,1, HTML.Tag.BR);

					// } catch (BadLocationException e1) {

					// // TODO Auto-generated catch block

					// e1.printStackTrace();

					// } catch (IOException e1) {

					// // TODO Auto-generated catch block

					// e1.printStackTrace();

					// }

					// InsertHTML("InsertBreak",

					// "<br>", ht, HTML.Tag.BR);

				}

			});

		}

		return insertMenu;

	}

	/**
	 * 
	 * @param args
	 * 
	 */

	public static void main(String[] args) {

		// TODO Auto-generated method stub

		Src_Html application = new Src_Html();

		application.show();

	}

	/**
	 * 
	 * This is the default constructor
	 * 
	 */

	public Src_Html() {

		super();

		initialize();

	}

	/**
	 * 
	 * This method initializes this
	 * 
	 * 
	 * 
	 * @return void
	 * 
	 */

	private void initialize() {

		this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

		this.setJMenuBar(getJJMenuBar());

		this.setSize(602, 436);

		this.setContentPane(getJContentPane());

		this.setTitle("Application");

	}

	/**
	 * 
	 * This method initializes jContentPane
	 * 
	 * 
	 * 
	 * @return javax.swing.JPanel
	 * 
	 */

	private javax.swing.JPanel getJContentPane() {

		if (jContentPane == null) {

			jContentPane = new javax.swing.JPanel();

			jContentPane.setLayout(new java.awt.BorderLayout());

			jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);

		}

		return jContentPane;

	}

	/**
	 * 
	 * This method initializes jJMenuBar
	 * 
	 * 
	 * 
	 * @return javax.swing.JMenuBar
	 * 
	 */

	private javax.swing.JMenuBar getJJMenuBar() {

		if (jJMenuBar == null) {

			jJMenuBar = new javax.swing.JMenuBar();

			jJMenuBar.add(getFileMenu());

			jJMenuBar.add(getEditMenu());

		}

		return jJMenuBar;

	}

	/**
	 * 
	 * This method initializes jMenu
	 * 
	 * 
	 * 
	 * @return javax.swing.JMenu
	 * 
	 */

	private javax.swing.JMenu getFileMenu() {

		if (fileMenu == null) {

			fileMenu = new javax.swing.JMenu();

			fileMenu.setText("File");

			fileMenu.add(getExitMenuItem());

		}

		return fileMenu;

	}

	/**
	 * 
	 * This method initializes jMenu
	 * 
	 * 
	 * 
	 * @return javax.swing.JMenu
	 * 
	 */

	private javax.swing.JMenu getEditMenu() {

		if (editMenu == null) {

			editMenu = new javax.swing.JMenu();

			editMenu.setText("Edit");

			editMenu.add(getMIshowSRC());

			editMenu.add(getInsertMenu());

		}

		return editMenu;

	}

	/**
	 * 
	 * This method initializes jMenuItem
	 * 
	 * 
	 * 
	 * @return javax.swing.JMenuItem
	 * 
	 */

	private javax.swing.JMenuItem getExitMenuItem() {

		if (exitMenuItem == null) {

			exitMenuItem = new javax.swing.JMenuItem();

			exitMenuItem.setText("Exit");

			exitMenuItem.addActionListener(new java.awt.event.ActionListener() {

				public void actionPerformed(java.awt.event.ActionEvent e) {

					System.exit(0);

				}

			});

		}

		return exitMenuItem;

	}

	/**
	 * 
	 * This method initializes jMenuItem
	 * 
	 * 
	 * 
	 * @return javax.swing.JMenuItem
	 * 
	 */

	private javax.swing.JMenuItem getMIshowSRC() {

		if (MIshowSRC == null) {

			MIshowSRC = new javax.swing.JMenuItem();

			MIshowSRC.setText("Show Source Code");

			MIshowSRC.addActionListener(new java.awt.event.ActionListener() {

				public void actionPerformed(java.awt.event.ActionEvent e) {

					try {

						if (jTP_SRC.isVisible()) {

							jTP_SRC.setVisible(false);

							jScrollPane.setViewportView(getJTP_HTML());

						} else {

							jTP_SRC.setVisible(true);

							jScrollPane.setViewportView(getJTP_SRC());

						}

					} catch (NullPointerException npe) {

						// jScrollPane.remove(getJTextPane());

						jScrollPane.setViewportView(getJTP_SRC());

					}

				}

			});

		}

		return MIshowSRC;

	}

	public void insertHTML(HTMLDocument doc, int offset, String html,

	int popDepth, int pushDepth, HTML.Tag insertTag)

	throws BadLocationException, IOException {

		Parser p = getParser();

		if (p == null) {

			throw new IOException("Can't load parser");

		}

		System.out.println("Document Laenge:::" + doc.getLength());

		System.out.println("Cursor Position:::" + offset);

		if (offset > doc.getLength()) {

			throw new BadLocationException("Invalid location", offset);

		}

		ParserCallback receiver = doc.getReader(offset);

		receiver.handleSimpleTag(HTML.Tag.BR, null, offset);

		//

		// ParserCallback receiver = doc.getReader(offset, popDepth, pushDepth,

		// insertTag);

		// Boolean ignoreCharset = (Boolean)
		// doc.getProperty("IgnoreCharsetDirective");

		// p.parse(new StringReader(html), receiver, (ignoreCharset == null) ?
		// false

		// : ignoreCharset.booleanValue());

		// receiver.flush();

	}

	/**
	 * 
	 * Fetch the parser to use for reading HTML streams. This can be
	 * reimplemented
	 * 
	 * to provide a different parser. The default implementation is loaded
	 * 
	 * dynamically to avoid the overhead of loading the default parser if it's
	 * not
	 * 
	 * used. The default parser is the HotJava parser using an HTML 3.2 DTD.
	 * 
	 */

	protected Parser getParser() {

		if (defaultParser == null) {

			try {

				Class c = Class
						.forName("javax.swing.text.html.parser.ParserDelegator");

				defaultParser = (Parser) c.newInstance();

			} catch (Throwable e) {

			}

		}

		return defaultParser;

	} // @jve:decl-index=0:visual-constraint="10,10"
}